//
//  SinaHomeOriginalView.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/27.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaHomeOriginalView: UIView {
    
    //记录原创微博底部的约束
    var originalViewBottomeCons: Constraint?
    
    //定义一个属性SinaStatusViewModel
    var statusViewModel: SinaStatusViewModel?{
        didSet{
           //当前控件赋值
            
            //头像
            headImageView.hm_setImage(withUrlString: statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            //昵称
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            //用户等级
            membershipImageView.image = statusViewModel?.mbrankImage
            //微博认证
            avatarImageView.image = statusViewModel?.verifiedImage
            //微博正文
            contentLabel.text = statusViewModel?.statusModel?.text
            //微博时间
            timeLabel.text = statusViewModel?.timeStr
//            //微博来源
            sourceLabel.text = statusViewModel?.sourceStr
            //配图赋值
            //卸载约束
            originalViewBottomeCons?.uninstall()
            //有配图
            if (statusViewModel?.statusModel?.pic_urls?.count)! > 0 {
                pictureView.picUrls = statusViewModel?.statusModel?.pic_urls
                pictureView.isHidden = false
                self.snp_makeConstraints(closure: { (make) in
                    self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
                })
            }else {
                //没有配图
                pictureView.isHidden = true
                self.snp_makeConstraints(closure: { (make) in
                    self.originalViewBottomeCons = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
            }
            
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        //01 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(membershipImageView)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //02 添加约束
        headImageView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 35, height: 35))
            make.top.left.equalTo(10)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.left.equalTo(headImageView.snp_right).offset(10)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(10)
        }
        
        membershipImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(10)
        }
        
        avatarImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp_right)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.left.equalTo(headImageView)
//            make.bottom.equalTo(self).offset(-10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            //如果 size 写死的话会导致 item 固定,只有这么大
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel)
        }

        self.snp_makeConstraints { (make) in
          self.originalViewBottomeCons = make.bottom.equalTo(pictureView).offset(10).constraint
        }
    }
    
    //MARK: - 懒加载控件
    //1.头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default_big")
    //2.昵称
    private lazy var nameLabel: UILabel = UILabel(text: "金克斯", fontSize: SinaNormalFontSize, textColor: UIColor.red)
    //3 时间
    private lazy var timeLabel: UILabel = UILabel(text: "时间", fontSize: SinaSmallFontSize, textColor: SinaColor)
    //4 来源
    private lazy var sourceLabel: UILabel = UILabel(text: "中央电视台 CCTV", fontSize: SinaSmallFontSize, textColor: SinaColor)
    //5 微博等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //6 认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //7.微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "", fontSize: SinaNormalFontSize, textColor: UIColor.black, maxWidth: SinaScreenW - 20)
    //8.配图
    private lazy var pictureView: SinaStatusPictureView = {
        let view = SinaStatusPictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

}
